home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 3 / BBS in a box - Trilogy III.iso / Files / System7 tools / Frontier / Frontier SDK 2.1 / Sample Code / Apple Events 101 / Client / client.c next >
Encoding:
C/C++ Source or Header  |  1993-05-22  |  2.5 KB  |  158 lines  |  [TEXT/KAHL]

  1.  
  2. /*© Copyright 1993 UserLand Software, Inc.  All Rights Reserved.*/
  3.  
  4. #include <AppleEvents.h>
  5.  
  6.  
  7. static void initMacintosh (void) {
  8.  
  9.     /*
  10.     the magic stuff that every Macintosh application needs to do 
  11.     before doing anything else.
  12.     */
  13.  
  14.     register short i;
  15.         
  16.     MaxApplZone ();
  17.     
  18.     for (i = 0; i < 10; i++) 
  19.         MoreMasters ();
  20.     
  21.     InitGraf (&qd.thePort);
  22.     
  23.     InitFonts ();
  24.     
  25.     FlushEvents (everyEvent, 0);
  26.     
  27.     InitWindows ();
  28.     
  29.     InitMenus ();
  30.     
  31.     TEInit ();
  32.     
  33.     InitDialogs (nil);
  34.     
  35.     InitCursor ();
  36.     
  37.     for (i = 0; i < 5; i++) { /*register with Multifinder*/
  38.         
  39.         EventRecord ev;
  40.         
  41.         EventAvail (everyEvent, &ev); /*see TN180 -- splash screen*/
  42.         } /*for*/
  43.     } /*initMacintosh*/
  44.  
  45.  
  46. static Boolean sendMessageToServer (OSType appid, OSType eventclass, OSType eventid, short i) {
  47.     
  48.     AEAddressDesc adr = {typeNull, nil}; 
  49.     AppleEvent event = {typeNull, nil};
  50.     AppleEvent reply = {typeNull, nil};
  51.     OSErr ec;
  52.     
  53.     /*1. create the Apple Event, address it to the server app*/ {
  54.     
  55.         ec = AECreateDesc (typeApplSignature, (Ptr) &appid, sizeof (appid), &adr);
  56.         
  57.         if (ec != noErr)
  58.             goto error;
  59.             
  60.         ec = AECreateAppleEvent (
  61.             eventclass, eventid, &adr, 
  62.             
  63.             kAutoGenerateReturnID, kAnyTransactionID, 
  64.             
  65.             &event);
  66.             
  67.         if (ec != noErr)
  68.             goto error;
  69.         }
  70.     
  71.     /*2. convert the number to a string, push it onto the Apple Event*/ {
  72.         
  73.         Str255 s;
  74.         
  75.         NumToString (i, s);
  76.         
  77.         if (AEPutParamPtr (&event, keyDirectObject, typeChar, (Ptr) &s [1], s [0]) != noErr)
  78.             goto error;
  79.         }
  80.         
  81.     /*3. send the message, wait for a reply*/ {
  82.     
  83.         ec = AESend (        
  84.             &event, &reply, kAEWaitReply + kAECanInteract + kAECanSwitchLayer, 
  85.             
  86.             kAENormalPriority, kNoTimeOut, nil, nil);
  87.             
  88.         if (ec != noErr)
  89.             goto error;
  90.         }
  91.     
  92.     /*4. make sure we got the correct reply*/ {
  93.     
  94.         AEDesc result;
  95.         DescType actualtype;
  96.         Size actualsize;
  97.         long num;
  98.  
  99.         ec = AEGetParamPtr (
  100.             &reply, keyDirectObject, typeLongInteger, 
  101.         
  102.             &actualtype, (Ptr) &num, sizeof (num), &actualsize);
  103.             
  104.         if (ec != noErr)
  105.             goto error;
  106.         
  107.         if (num != i) {
  108.             
  109.             DebugStr ("\pThe reply is not what we expected.");
  110.             
  111.             goto error;
  112.             }
  113.         }
  114.     
  115.     AEDisposeDesc (&adr);
  116.         
  117.     AEDisposeDesc (&event);    
  118.     
  119.     AEDisposeDesc (&reply);    
  120.     
  121.     return (true);
  122.     
  123.     error:
  124.     
  125.     /*report the error code using DebugStr*/ {
  126.         
  127.         Str255 s;
  128.         
  129.         NumToString (ec, s);
  130.         
  131.         DebugStr (s);
  132.         }
  133.     
  134.     AEDisposeDesc (&adr);
  135.         
  136.     AEDisposeDesc (&event);    
  137.     
  138.     AEDisposeDesc (&reply);    
  139.     
  140.     return (false);
  141.     } /*sendMessageToServer*/
  142.     
  143.     
  144. void main (void) {
  145.     
  146.     short i;
  147.     
  148.     initMacintosh ();
  149.     
  150.     for (i = 1; i <= 100; i++) 
  151.         if (!sendMessageToServer ('AESV', 'SERV', 'DISP', i))
  152.             break;
  153.     } /*main*/
  154.  
  155.  
  156.  
  157.  
  158.